package info.openlvb.openface.client.handler;

import info.openlvb.openface.client.base.person.ListGroupPanel;
import info.openlvb.openface.client.base.person.MemberInGroupWindow;
import info.openlvb.openface.client.service.LdapService;
import info.openlvb.openface.client.service.LdapServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

public class DeleteGroupListener extends ButtonListenerAdapter {

	private final LdapServiceAsync ldapService = GWT.create(LdapService.class);
	private ListGroupPanel listGroupPanel;
	private String groupname;
	private MemberInGroupWindow memberInGroupWindow;
	DialogBox dialog;
	
	public DeleteGroupListener(MemberInGroupWindow memberInGroupWindow, ListGroupPanel listGroupPanel, String groupname) {
		this.listGroupPanel = listGroupPanel;
		this.groupname = groupname;
		this.memberInGroupWindow = memberInGroupWindow;
	}

	public void onClick(Button button, EventObject e) {
		dialog = new DialogBox();
		dialog.setTitle("Confirmation de suppression");
		dialog.setAnimationEnabled(true);
		dialog.setGlassEnabled(true);
		
		VerticalPanel content = new VerticalPanel();
		HTML lbl_message = new HTML("Voulez-vous vaiment supprimer ce groupe ?</br>");
		content.add(lbl_message);
		Button yesBtn = new Button("Valider");
		yesBtn.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e){
				ldapService.removeGroup(groupname, new AsyncCallback<Void>() {
					public void onSuccess(Void result) {
						listGroupPanel.refresh();
						dialog.hide();
						memberInGroupWindow.hide();
					}
					public void onFailure(Throwable caught) {
						MessageBox.alert("probleme de liaison vers le backend");
					}
				});
			}
		});
		
		Button noBtn = new Button("Abandonner");
		noBtn.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				dialog.hide();
			}			
		});
		noBtn.setStyle("closeBtn");
		Panel btPanel = new Panel();
		btPanel.addButton(yesBtn);
		btPanel.addButton(noBtn);
		content.add(btPanel);
		dialog.add(content);
		dialog.center();
		dialog.show();
	}
}
